//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.example.demo.component;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.support.AbstractBeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.support.AbstractRefreshableApplicationContext;
import org.springframework.context.support.GenericApplicationContext;
import org.springframework.core.io.DefaultResourceLoader;

import java.io.IOException;

public class SpringContextHolder implements ApplicationContextAware, DisposableBean {
    private static ApplicationContext applicationContext = null;
    private static Logger logger = LoggerFactory.getLogger(SpringContextHolder.class);
    private static final String MPPROPS_BEAN_CLASS = "com.ly.mp.component.conf.MpProperties";
    private static final String MPPROPS_BEAN_ID = "mpProperties";
    private static final String ID_KEY = "id";

    public SpringContextHolder() {
    }

    public static ApplicationContext getApplicationContext() {
        assertContextInjected();
        return applicationContext;
    }

    public static String getRootRealPath() {
        String rootRealPath = "";

        try {
            rootRealPath = getApplicationContext().getResource("").getFile().getAbsolutePath();
        } catch (IOException var2) {
            logger.warn("获取系统根目录失败");
        }

        return rootRealPath;
    }

    public static String getResourceRootRealPath() {
        String rootRealPath = "";

        try {
            rootRealPath = (new DefaultResourceLoader()).getResource("").getFile().getAbsolutePath();
        } catch (IOException var2) {
            logger.warn("获取资源根目录失败");
        }

        return rootRealPath;
    }

    public static <T> T getBean(String name) {
        assertContextInjected();
        return (T) applicationContext.getBean(name);
    }

    public static <T> T getBean(String name, Class<T> requiredType) {
        assertContextInjected();
        return applicationContext.getBean(name, requiredType);
    }

    public static <T> T getBean(Class<T> requiredType) {
        assertContextInjected();
        return applicationContext.getBean(requiredType);
    }

    public static void clearHolder() {
        if (logger.isDebugEnabled()) {
            logger.debug("清除SpringContextHolder中的ApplicationContext:" + applicationContext);
        }

        applicationContext = null;
    }

    public void setApplicationContext(ApplicationContext applicationContext) {
        if (SpringContextHolder.applicationContext != null) {
            logger.info("SpringContextHolder中的ApplicationContext被覆盖, 原有ApplicationContext为:" + SpringContextHolder.applicationContext);
        }

        this.loadBeans(applicationContext);
        SpringContextHolder.applicationContext = applicationContext;
    }

    public void destroy() throws Exception {
        clearHolder();
    }

    private static void assertContextInjected() {
    }

    protected void loadBeans(ApplicationContext applicationContext) {
        BeanDefinitionBuilder beanBuilder = BeanDefinitionBuilder.rootBeanDefinition("com.ly.mp.component.conf.MpProperties");
        beanBuilder.getBeanDefinition().setAttribute("id", "mpProperties");
        this.registerBeanDefinition(applicationContext, beanBuilder.getBeanDefinition());
    }

    private void registerBeanDefinition(ApplicationContext applicationContext, AbstractBeanDefinition beanDefinition) {
        DefaultListableBeanFactory beanFactory = this.getBeanFactory(applicationContext);
        String beanId = beanDefinition.getAttribute("id").toString();
        if (!beanFactory.containsBeanDefinition(beanId)) {
            beanFactory.registerBeanDefinition(beanId, beanDefinition);
        }

    }

    private DefaultListableBeanFactory getBeanFactory(ApplicationContext applicationContext) {
        if (applicationContext instanceof AbstractRefreshableApplicationContext) {
            AbstractRefreshableApplicationContext springContext = (AbstractRefreshableApplicationContext)applicationContext;
            if (!(springContext.getBeanFactory() instanceof DefaultListableBeanFactory)) {
                logger.error("No suitable bean factory! The current factory class is {}", springContext.getBeanFactory().getClass());
            }

            return (DefaultListableBeanFactory)springContext.getBeanFactory();
        } else if (applicationContext instanceof GenericApplicationContext) {
            GenericApplicationContext springContext = (GenericApplicationContext)applicationContext;
            return springContext.getDefaultListableBeanFactory();
        } else {
            logger.error("No suitable application context! The current context class is {}", applicationContext.getClass());
            return null;
        }
    }
}
